using System.IO;
using MEA.AE_Data.Config;

namespace MEA.AE_BusinessLogic
{
	public class ScriptingInterface
	{
		private ServiceLayer servicelayer;

		public ScriptingInterface(ServiceLayer servicelayer)
		{
			this.servicelayer = servicelayer;
		}

		public void EEPROM_Backup(string filename)
		{
			if (servicelayer.ECU_VerifyBothConnected())
			{
				string text = ConfigData.LogRootPath + "\\Flash";
				Directory.CreateDirectory(text);
				Stream str = new FileStream(text + "\\" + filename, FileMode.Create);
				servicelayer.EEPROM_Backup_SaveXML(str);
			}
		}

		public void EEPROM_Restore(string filename)
		{
		}

		public void DTC_Clear()
		{
			if (!servicelayer.BaseServiceA.EcuData.DTCReadOut || !servicelayer.BaseServiceB.EcuData.DTCReadOut)
			{
				servicelayer.DTC_Read();
			}
			servicelayer.DTC_Clear(false);
		}

		public void DTC_Read()
		{
			servicelayer.DTC_Read();
		}

		public void EngLog_Save(string filename)
		{
			servicelayer.EngLogSave(ConfigData.LogRootPath + "\\Engine_Log\\" + filename);
		}

		public void ECU_Connect()
		{
			servicelayer.ECU_Connect();
		}

		public void ECU_Disconnect()
		{
			servicelayer.ECU_Disconnect();
		}

		public void Eng_SerialWrite(string sn)
		{
			servicelayer.EngineSerialWrite(sn);
		}

		public void Eng_UptimeReset()
		{
			servicelayer.EngineUptimeReset();
		}

		public void Eng_SerialReRead()
		{
			servicelayer.EngineSerialRead();
		}

		public string Eng_SerialRead()
		{
			string engineSerialNumber = servicelayer.BaseServiceB.EcuData.EngineSerialNumber;
			string[] array = engineSerialNumber.Split('-');
			if (array.Length == 3)
			{
				engineSerialNumber = servicelayer.BaseServiceA.EcuData.EngineSerialNumber;
			}
			return engineSerialNumber;
		}

		public void RecMan_ConfigSave(string logpath)
		{
		}

		public void RecMan_ConfigLoad(string logpath)
		{
		}

		public void RecMan_Format()
		{
			servicelayer.RecManFormatScr();
		}

		public void RTC_Write(string rtc)
		{
			servicelayer.RTCWrite(rtc);
		}

		public void Stat_Reset()
		{
			servicelayer.Statistics_Reset();
		}

		public void IQAInitialize2Script()
		{
			servicelayer.IQA_Init2Script();
		}

		public void IQA_CylRead()
		{
			servicelayer.IQA_CylRead(ConfigData.e_IQACylinder.ALL);
		}

		public string[] IQA_CylReadData()
		{
			return servicelayer.BaseServiceA.EcuData.IQACylRead;
		}

		public void IQA_CylWriteAll(string iqaDataStr)
		{
			string[] instr = iqaDataStr.Split(',');
			if (instr.Length == 4)
			{
				if (instr[0].Length == 6)
				{
					IMA iMA = new IMA(ConfigData.EIMACodeFrm.eIMACode6Lan, ref instr);
					servicelayer.IQA_CylWrite(iMA.toSendList);
				}
				else if (instr[0].Length == 7)
				{
					IMA iMA = new IMA(ConfigData.EIMACodeFrm.eIMACode7Lan, ref instr);
					servicelayer.IQA_CylWrite(iMA.toSendList);
				}
			}
		}

		public void IQA_CylWrite(int cyl, string iqaData)
		{
			string[] array = new string[4];
			string[] instr = array;
			if (cyl >= 0 && cyl <= 4)
			{
				instr[cyl] = iqaData;
				if (iqaData.Length == 6)
				{
					IMA iMA = new IMA(ConfigData.EIMACodeFrm.eIMACode6Lan, ref instr);
					servicelayer.IQA_CylWrite(iMA.toSendList);
				}
				else if (iqaData.Length == 7)
				{
					IMA iMA = new IMA(ConfigData.EIMACodeFrm.eIMACode7Lan, ref instr);
					servicelayer.IQA_CylWrite(iMA.toSendList);
				}
			}
		}

		public void IgnoreFlashCond(int flashCond)
		{
			if (flashCond == 0)
			{
				ConfigFlash.IgnoreFlashCond = false;
			}
			else
			{
				ConfigFlash.IgnoreFlashCond = true;
			}
		}

		public void RecManFormat()
		{
			servicelayer.RecManFormat();
		}

		public void DatLog_SaveXML(string logpath, int minSector, int maxSector)
		{
			if (logpath.Length > 3 && minSector <= maxSector && minSector >= 0 && maxSector < 156)
			{
				servicelayer.DatLog_SaveXML(logpath, minSector, maxSector);
			}
		}
	}
}
